﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;
using System.Collections.Specialized;
using Maticsoft.Model;

public partial class WebPage_ajaxPage_Type : System.Web.UI.Page
{
    // Fields

    private Maticsoft.BLL. Category category = new Maticsoft.BLL.Category();
    protected string cid = string.Empty;

    public string message = "";

    protected string zone = string.Empty;

    // Methods
    private void insertValue()
    {
        Category model = new Category();
        model.Name = this.name.Value;
        model.Ename = this.ename.Value;
        if (this.letter.Value != string.Empty)
        {
            model.Letter = this.letter.Value.ToUpper();
        }
        else
        {
            model.Letter = "N";
        }
        model.Czone = this.czone.Value;
        model.Zone = this.zone;
        model.Display = this.display.Value.ToUpper();
        model.Sort_order = int.Parse(this.sort_order.Value);
        if (this.category.Add(model) > 0)
        {
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        this.cid = Helper.GetString(base.Request["id"], string.Empty);
        this.zone = Helper.GetString(base.Request["zone"], string.Empty);
        if (base.Request.HttpMethod != "POST")
        {
            if (base.Request["Zone"] != null)
            {
                this.bian.Visible = false;
                this.mingcheng.InnerText = this.setMing(base.Request["Zone"].ToString());
            }
            if (base.Request["update"] != null)
            {
                this.xin.Visible = false;
                this.setValue(int.Parse(base.Request["update"].ToString()));
            }
        }
        else if (base.Request.Form["buttontype"] == "确定")
        {
            if (this.cid == "")
            {
                this.insertValue();
            }
            else
            {
                this.updateValue();
            }
            base.Response.Redirect(base.Request.UrlReferrer.AbsoluteUri);
            base.Response.End();
        }
    }

    private string setMing(string name)
    {
        string str = "";
        if (name == "public")
        {
            str = "讨论区分类";
        }
        else if (name == "city")
        {
            str = "城市分类";
        }
        else if (name == "express")
        {
            str = "快递公司分类";
        }
        else if (name == "partner")
        {
            str = "商户分类";
        }
        else if (name == "group")
        {
            str = "项目分类";
        }
        else if (name == "grade")
        {
            str = "用户等级分类";
        }
        this.zone = name;
        return str;
    }

    private void setValue(int id)
    {
        foreach (Category category in this.category.GetModelList("Id=" + id))
        {
            this.name.Value = category.Name;
            this.ename.Value = category.Ename;
            this.letter.Value = category.Letter;
            if (category.Display == "N")
            {
                this.display.Value = "";
                this.display.Value = "N";
            }
            else
            {
                this.display.Value = "";
                this.display.Value = "Y";
            }
            this.sort_order.Value = category.Sort_order.ToString();
            this.cid = category.Id.ToString();
            this.czone.Value = category.Czone;
            this.zone = category.Zone;
        }
    }

    private void updateValue()
    {
        Category model = new Category();
        model.Id = int.Parse(this.cid);
        model.Name = this.name.Value;
        model.Ename = this.ename.Value;
        if (this.letter.Value != string.Empty)
        {
            model.Letter = this.letter.Value.ToUpper();
        }
        else
        {
            model.Letter = "N";
        }
        model.Letter = this.letter.Value;
        model.Czone = this.czone.Value;
        model.Zone = this.zone;
        model.Display = this.display.Value.ToUpper();
        model.Sort_order = int.Parse(this.sort_order.Value);
        this.category.Update(model);
    }

}